layui.use(['form', 'layer'], function(){
    var form = layui.form;
    var layer = layui.layer;

    // 注册按钮点击事件
    $('#registerBtn').on('click', function(){
        layer.open({
            type: 1,
            title: '用户注册',
            content: $('#registerFormTemplate').html(),
            area: ['420px', '420px'],
            success: function(layero, index){
                form.render();

                // 自定义密码校验规则
                form.verify({
                    confirmPass: function(value){
                        if($('#registerForm [name=password]').val() !== value){
                            return '两次密码输入不一致';
                        }
                    }
                });

                // 注册表单提交
                form.on('submit(registerSubmit)', function(data){
                    var formData = data.field;
                    ajaxHttp({
                        url: '/uaa/register',
                        type: 'POST',
                        data: JSON.stringify({
                            phone: formData.phone,
                            password: formData.password
                        }),
                    }, function(res){
                        if(res.code === 200){
                            layer.msg('注册成功', {icon: 1});
                            setTimeout(() => layer.close(index), 1500);
                        } else {
                            layer.msg(res.msg || '注册失败', {icon: 2});
                        }
                    });
                    return false;
                });
            }
        });
    });
});

